package com.example.administrator.hqq.adapter.discover;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.FitCenter;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.adapter.SetOnClickItem;
import com.example.administrator.hqq.bean.ConventionListBean;
import com.example.administrator.hqq.utils.transform.GlideCircleTransform;
import com.example.administrator.hqq.utils.transform.GlideRoundTransform;
import com.zhy.autolayout.AutoLinearLayout;

import java.lang.reflect.Field;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * @name: HQQ
 * @describe:
 * @author: 康海涛 QQ2541849981
 * @time: 2018/11/5 13:01
 * @remark:
 */
public class ConvenGridAdapter extends BaseAdapter {
    private Context context;
    private List<ConventionListBean.DataBean> list;
    private boolean aBoolean;
    private SetOnClickItem setOnClickItem;

    public void setSetOnClickItem(SetOnClickItem setOnClickItem) {
        this.setOnClickItem = setOnClickItem;
    }

    public ConvenGridAdapter(Context context, List<ConventionListBean.DataBean> list, boolean aBoolean) {
        this.context = context;
        this.list = list;
        this.aBoolean=aBoolean;
    }

    public void setaBoolean(boolean aBoolean) {
        this.aBoolean = aBoolean;
    }

    @Override
    public int getCount() {
        return list.size()+1;
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
    private GridView gv;
    private int[] mostRowHeight;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @SuppressLint("ViewHolder")
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.conven_grid_item, viewGroup, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        Log.e("kang" , "position :    "+i +"list.size    "+list.size());
        viewHolder.addModule.setVisibility(View.GONE);
        if (list.size()==i){
            viewHolder.autoBorder.setVisibility(View.GONE);
            viewHolder.addModule.setVisibility(View.VISIBLE);
            viewHolder.moduleIv.setVisibility(View.GONE);
            viewHolder.delete.setVisibility(View.GONE);
            viewHolder.moduleName.setText("");
        }else{
            viewHolder.autoBorder.setVisibility(View.VISIBLE);
            viewHolder.addModule.setVisibility(View.GONE);
            viewHolder.moduleIv.setVisibility(View.VISIBLE);
            if (aBoolean){
                viewHolder.delete.setVisibility(View.VISIBLE);
            }
            viewHolder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (setOnClickItem!=null)
                    {
                        setOnClickItem.setOnClickItem(i);
                    }
                }
            });
            viewHolder.moduleName.setText(list.get(i).getName());
            Glide.with(context).load(list.get(i).getUrl())
                    .bitmapTransform(new RoundedCornersTransformation(context, 40, 0,
                            RoundedCornersTransformation.CornerType.ALL))
                    .into(viewHolder.moduleIv);
        }

        return view;
    }
    static class ViewHolder {
        @BindView(R.id.module_iv)
        ImageView moduleIv;
        @BindView(R.id.delete)
        ImageView delete;
        @BindView(R.id.add_module)
        ImageView addModule;
        @BindView(R.id.module_name)
        TextView moduleName;
        @BindView(R.id.auto_border)
        AutoLinearLayout autoBorder;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
